package com.allenlee.mvphelper.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author : lifei
 * 创建时间： 19-12-6
 * 更改时间： 19-12-6
 * Email : allen.lee723@qq.com
 * @version :
 * Description：
 */
public class ByteHelper {

    /**
     * 功能描述: byte数组转 InputStream
     * @param bytes byte数组
     * @return java.io.InputStream
     * @author xiaobu
     * @date 2019/3/28 16:01
     * @version 1.0
     */
    public static InputStream byte2InputStream(byte[] bytes) {
        return new ByteArrayInputStream(bytes);
    }

    /**
     * 功能描述:
     * @param inputStream 输入流
     * @return byte[] 数组
     * @author xiaobu
     * @date 2019/3/28 16:03
     * @version 1.0
     */
    public static byte[] inputStream2byte(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte[] buff = new byte[1024];
        int rc;
        while ((rc = inputStream.read(buff)) > 0) {
            byteArrayOutputStream.write(buff, 0, rc);
        }
        return byteArrayOutputStream.toByteArray();
    }

    /**
     * Description: 负数byte转正int <BR>
     * @param b
     * @return
     * @author dsn
     * @date 2018年12月29日 下午3:29:38
     * @version 1.0
     */
    public static Integer byteToInteger(Byte b) {
        return 0xff & b;
    }

    /**
     * int到byte[]
     * @param i 整数
     * @return byte 数组
     */
    public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        // 由高位到低位
        result[0] = (byte) ((i >> 24) & 0xFF);
        result[1] = (byte) ((i >> 16) & 0xFF);
        result[2] = (byte) ((i >> 8) & 0xFF);
        result[3] = (byte) (i & 0xFF);
        return result;
    }

    /**
     * byte[]转int
     * @param bytes byte 数组
     * @return 整数
     */
    public static int byteArrayToInt(byte[] bytes) {
        bytes = Arrays.copyOf(bytes, 4);
        int value = 0;
        int length = bytes.length;
        System.out.println("length = " + length);
        // 由高位到低位
        for (int i = 0; i < length; i++) {
            int shift = (length - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;// 往高位游
        }

        return value;
    }

    public static byte[] arrayReverse(byte[] bytes) {
        byte temp;
        for (int i = 0; i < bytes.length / 2; i++) {
            temp = bytes[i];
            bytes[i] = bytes[bytes.length - i - 1];
            bytes[bytes.length - i - 1] = temp;
        }
        return bytes;
    }

    public static void main(String[] args) {
        String str = "你好呀";
        byte[] bytes = str.getBytes();
        InputStream inputStream = byte2InputStream(bytes);
        try {
            byte[] bytes1 = inputStream2byte(inputStream);
            String string = new String(bytes1, StandardCharsets.UTF_8);
            System.out.println("string = " + string);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
